package com.yunxin.core.utils.infra.utils;

import cn.hutool.core.text.CharSequenceUtil;
import com.yunxin.core.utils.infra.common.constant.TimeZoneConstant;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * @author zero°
 * @since 2023-06-13
 */
@Slf4j
public enum DateFormatUtils {

    /**
     *
     */
    X;

    public static final String DATE_SECOND_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_BAR_FORMAT = "yyyy-MM-dd";

    public static final String TIME_END = " 23:59:59";
    public static final String TIME_START = " 00:00:00";


    /**
     * 获取开始时间
     * @param timeStr timeStr
     * @return Date
     */
    public Date getDateByStartTimeStr(String timeStr) {
        if (CharSequenceUtil.isEmpty(timeStr)) {
            return null;
        }
        return getDateFromFullStr(timeStr + TIME_START, DATE_SECOND_FORMAT);
    }

    /**
     * 获取结束时间
     * @param timeStr timeStr
     * @return Date
     */
    public Date getDateByEndTimeStr(String timeStr) {
        if (CharSequenceUtil.isEmpty(timeStr)) {
            return null;
        }
        return getDateFromFullStr(timeStr + TIME_END, DATE_SECOND_FORMAT);
    }

    /**
     * 获取时间字符串(yyyy-MM-dd)
     * @return DateStr
     */
    public String getDateStrByNow() {
        SimpleDateFormat format = new SimpleDateFormat(DATE_BAR_FORMAT);
        return format.format(new Date());
    }

    /**
     * 获取时间字符串(yyyy-MM-dd HH:mm:ss)
     * @return DateStr
     */
    public String getFullDateStrByNow() {
        SimpleDateFormat format = new SimpleDateFormat(DATE_SECOND_FORMAT);
        return format.format(new Date());
    }

    /**
     * 获取时间字符串(yyyy-MM-dd)
     * @return DateStr
     */
    public String getDateStr(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(DATE_BAR_FORMAT);
        return format.format(date);
    }


    /**
     * 获取传入时间的当天开始时间
     * @return Date
     */
    public Date getDateByStartDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_BAR_FORMAT);
        return getDateFromFullStr(format.format(date) + TIME_START, DATE_SECOND_FORMAT);
    }

    /**
     * 获取传入时间的当天结束时间
     * @return Date
     */
    public Date getDateByEndDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_BAR_FORMAT);
        return getDateFromFullStr(format.format(date) + TIME_END, DATE_SECOND_FORMAT);
    }


    /**
     * 获取时间(yyyy-MM-dd)
     * @param timeStr timeStr
     * @return Date
     */
    public Date getDateByStr(String timeStr) {
        if (CharSequenceUtil.isEmpty(timeStr)) {
            return null;
        }
        return getDateFromFullStr(timeStr, DATE_BAR_FORMAT);
    }

    /**
     * 获取时间(yyyy-MM-dd/yyyy-MM-dd HH:mm:ss)
     * @param timeStr timeStr
     * @return Date
     */
    public Date getDateOrTimeByStr(String timeStr) {
        if (CharSequenceUtil.isEmpty(timeStr)) {
            return null;
        }
        String format = DATE_BAR_FORMAT;
        if (timeStr.length() > DATE_BAR_FORMAT.length()) {
            format = DATE_SECOND_FORMAT;
        }
        return getDateFromFullStr(timeStr, format);
    }

    /**
     * 前几天时间
     *
     * @param date date
     * @param day  day
     * @return yyyy-MM-dd
     */
    public Date getDayLater(Date date, int day) {
        OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(date.toInstant(), TimeZoneConstant.CHINA.getOffset());
        LocalDateTime twoMinutesLater = offsetDateTime.toLocalDateTime().plusDays(-day);
        return Date.from(twoMinutesLater.toInstant(TimeZoneConstant.CHINA.getOffset()));
    }

    public Date getDateFromFullStr(String timeStr, String rex) {
        try {
            if (CharSequenceUtil.isEmpty(timeStr)) {
                return null;
            }
            SimpleDateFormat format = new SimpleDateFormat(rex);
            return format.parse(timeStr);
        } catch (Exception e) {
            log.error("DateUtils getDateFromFullStr", e);
            return null;
        }
    }

}

